package com.marvels.approve.loan.controller;

import com.alibaba.fastjson.JSON;
import com.marvels.approve.common.dto.InsurancePolicyDTO;
import com.marvels.approve.loan.api.LoanApplyProvideApi;
import com.marvels.approve.loan.dto.McamLoanApplyContactInfo;
import com.marvels.approve.loan.dto.McamLoanApplyInfoDTO;
import com.marvels.approve.loan.dto.WaitLendInfoDTO;
import com.marvels.approve.loan.service.IMcamLoanApplyInfoService;
import com.marvels.approve.process.dto.ProtocolTemplateInfoDTO;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 审批提供给外部调用
 * 
 * @author tangpw
 */
@RequestMapping("/approve/loan/api")
@RestController
public class ApproveProvideController {

	@Autowired
	private LoanApplyProvideApi loanApplyProvideApi;
	@Autowired
	private IMcamLoanApplyInfoService mcamLoanApplyInfoService ;

	@RequestMapping("modifyLoanApplyInfo")
	@ResponseBody
	public McamLoanApplyInfoDTO modifyLoanApplyInfo(@RequestBody McamLoanApplyInfoDTO mcamLoanApplyInfoDTO) {
		MarvelsLogUtil.getInstance().info("ApproveProvideController api modifyLoanApplyInfo param==>"+JSON.toJSONString(mcamLoanApplyInfoDTO));
		loanApplyProvideApi.modifyLoanApplyInfo(mcamLoanApplyInfoDTO);
		return mcamLoanApplyInfoDTO;
	}

	@RequestMapping("selectLoanApplyInfo")
	@ResponseBody
	public McamLoanApplyInfoDTO selectLoanApplyInfo(@RequestBody McamLoanApplyInfoDTO mcamLoanApplyInfoDTO) {
		return loanApplyProvideApi.selectLoanApplyInfo(mcamLoanApplyInfoDTO);
	}

	@RequestMapping("selectContactByLoanId")
	@ResponseBody
	public List<McamLoanApplyContactInfo> selectContactByLoanId(
			@RequestBody McamLoanApplyContactInfo mcamLoanApplyContactInfo) {
		return loanApplyProvideApi.selectContactByLoanId(mcamLoanApplyContactInfo);
	}

	@RequestMapping("selectInsuranceByLoanId")
	@ResponseBody
	public List<InsurancePolicyDTO> selectInsuranceByLoanId(
			@RequestBody InsurancePolicyDTO mcamLoanInsurancePolicyInfo) {
		return loanApplyProvideApi.selectInsuranceByLoanId(mcamLoanInsurancePolicyInfo);
	}

	/**
	 * 根据产品类别 资金源code查询待放款的进件
	 * @param mcamLoanApplyInfoDTO
	 * @return
	 */
	@RequestMapping("selectWaitLendInfo")
	@ResponseBody
	public List<String> selectWaitLendInfo(@RequestBody WaitLendInfoDTO infoDTO) {
		return loanApplyProvideApi.selectWaitLendInfo(infoDTO);
	}

	/**退回补件
	 * @param dto  其中loanId必须输入
	 * @return
	 */
	@RequestMapping("fallback")
	@ResponseBody
	public MarvelsResultDTO loanFallback(@RequestBody McamLoanApplyInfoDTO dto){
		try {
			mcamLoanApplyInfoService.loanFallback(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), "");
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), null);
		} 
	}
	
	/**
	 * 通过资金源查询签名合同模板
	 * @param infoDTO
	 * @return
	 */
	@RequestMapping("findProtocalTemplateList")
	@ResponseBody
	public List<ProtocolTemplateInfoDTO> findProtocalTemplateList(@RequestBody McamLoanApplyInfoDTO infoDTO) {
		return loanApplyProvideApi.findProtocalTemplateList(infoDTO);
	}
}
